import 'package:flutter/material.dart';
import 'package:date_format/date_format.dart' as DateFormat;

class DatePickerDemo extends StatefulWidget {
  DatePickerDemo({Key key}) : super(key: key);

  @override
  _DatePickerDemoState createState() => _DatePickerDemoState();
}

class _DatePickerDemoState extends State<DatePickerDemo> {

  DateTime _nowDate = DateTime.now();
  var _nowTime = TimeOfDay(hour: 22, minute: 00);

  _showDatePicker() async{
    // showDatePicker(
    //   context: context, 
    //   initialDate: _nowDate, 
    //   firstDate: DateTime(1980), 
    //   lastDate: DateTime(2100)
    // ).then((result){
    //   print(result);
    // });

    var result = await showDatePicker(
      context: context, 
      initialDate: _nowDate, 
      firstDate: DateTime(1980), 
      lastDate: DateTime(2100),
      locale: Locale('zh')
    );
    print(result);

    setState(() {
      this._nowDate = result;
    });
  }

  _showTimePicker() async{
    var result = await showTimePicker(
      context: context, 
      initialTime: _nowTime
    );

    setState(() {
      this._nowTime = result;
    });
  }

  @override
  void initState() {
    super.initState();
    
    // var now = DateTime.now();
    // print(now); //2020-06-18 21:22:29.466070
    // print(now.millisecondsSinceEpoch); //1592486593281
    // print(DateTime.fromMillisecondsSinceEpoch(1592486593281)); //2020-06-18 21:23:13.281
    // print(formatDate(DateTime.now(), [yyyy, '年', mm, '月', dd, '日']));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DatePicker'), 
      ), 
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              InkWell(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text('${DateFormat.formatDate(_nowDate, ['yyyy', '年', 'mm', '月', 'dd', '日'])}'),
                    Icon(Icons.arrow_drop_down)
                  ]
                ),
                onTap: this._showDatePicker,
              ),
              InkWell(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text('${this._nowTime.format(context)}'),
                    Icon(Icons.arrow_drop_down)
                  ]
                ),
                onTap: _showTimePicker,
              )
            ], 
          )
        ], 
      ),
    );
  }
}